#include <stdio.h>
#include <stdlib.h>
#include <jpeglib.h>

void print_jpeg_info(const char *filename) {
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;

    // 设置错误处理
    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);

    // 打开 JPEG 文件
    FILE *infile = fopen(filename, "rb");
    if (infile == NULL) {
        fprintf(stderr, "Cannot open %s\n", filename);
        return;
    }

    // 读取 JPEG 文件头
    jpeg_stdio_src(&cinfo, infile);
    jpeg_read_header(&cinfo, TRUE);

    // 打印 JPEG 信息
    printf("JPEG Image Information:\n");
    printf("Width: %d pixels\n", cinfo.image_width);
    printf("Height: %d pixels\n", cinfo.image_height);
    printf("Color Components: %d\n", cinfo.num_components);
    // printf("JPEG Quality: %d\n", cinfo.jpeg_quality); // 移除这一行
    printf("Progressive: %s\n", cinfo.progressive_mode ? "Yes" : "No");

    // 释放资源
    jpeg_destroy_decompress(&cinfo);
    fclose(infile);
}

int main(int argc, char *argv[]) {
    if (argc < 2) {
        fprintf(stderr, "Usage: %s <input.jpg>\n", argv[0]);
        return 1;
    }

    print_jpeg_info(argv[1]);
    return 0;
} 


/*
sudo apt-get install libjpeg-dev
gcc read_jpeg_info.c -o read_jpeg_info -ljpeg
./read_jpeg_info input.jpg
*/